package com.lcq.study.pdf.jasper;

import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @ClassName JasperReportTest
 * @Description
 * @Author lichuanqi
 * @Date 2022/6/24 10:07
 * @Version 1.0
 **/
public class JasperReportPagesTest {
    public static void main(String[] args) throws JRException {
        HashMap<String, Object> params = new HashMap<>();
        params.put("accountNum","中国建筑东北设计研究院");
        params.put("prjName","东北院项目");
        params.put("fileName","ZH-WJF-YF-33#-S-SG");
        params.put("forecastIndex","颗粒物、光照、微气候、能耗、碳排放");
        params.put("reportDate","2022 年 6 月 8 日");
        String jasperFilePath = "C:/Users/86156/JaspersoftWorkspace/MyReports";
        String jasperFile = jasperFilePath + "/Blank_A4.jasper";
        String destFile = "D:/test/pdf/jasper_test.pdf";

        JasperFillManager jasperFillManager = JasperFillManager.getInstance(DefaultJasperReportsContext.getInstance());

        /**/
        JasperPrint jasperPrint = jasperFillManager.fill(jasperFile, params, new JREmptyDataSource());

        HashMap<String, Object> params2 = new HashMap<>();
        String jasperFile2 = jasperFilePath + "/Blank_A4_1.jasper";
        JasperPrint jasperPrint2 = jasperFillManager.fill(jasperFile2, params2, new JREmptyDataSource());

        List<JasperPrint> jasperPrintList = new ArrayList<>();
        jasperPrintList.add(jasperPrint);
        jasperPrintList.add(jasperPrint2);

        exportPages(destFile, jasperPrintList);

    }

    private static void exportPages(String destFile, List<JasperPrint> jasperPrintList) throws JRException {
        JRPdfExporter exporter = new JRPdfExporter(DefaultJasperReportsContext.getInstance());
        SimpleExporterInput simpleExporterInput = SimpleExporterInput.getInstance(jasperPrintList);
        exporter.setExporterInput(simpleExporterInput);
        exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(destFile));
        exporter.exportReport();
    }
}
